<style lang="stylus" scoped>
@import '../../styl/var.styl'

main
  display flex

:deep(li)
  list-style none

:deep(.bk)
  margin-top 7px
  flex-direction column

  &, &>h2
    font-size 14px

  & > h2
    color gray
    font-weight normal
    margin 0 0 8px

  .select
    position relative
    flex 1

    select
      // https://blog.csdn.net/zhangchen124/article/details/79634842
      width 0
      flex 1
      background-image url('!/svg/a.d.svg')
      background-position calc(100% - 16px) 50%
      background-repeat no-repeat
      -webkit-appearance none
      white-space nowrap
      border-radius 8px
      border solid 1px #e9ebed
      height 54px
      padding 9px 31px 9px 17px

      &[disabled]
        background-image linear-gradient(rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.5)), url('!/svg/a.d.svg')
        background-position 0 0, calc(100% - 16px) 50%
        cursor not-allowed
</style>

<template lang="pug">
src
main
  r-area
  drive
  btn(@click="start") {{ txt }}
</template>

<script lang="coffee">
import src from '~/vue/record/src.vue'
import rArea from '~/vue/record/area.vue'
import drive from '~/vue/record/drive.vue'
import btn from '~/vue/record/start.vue'

import start from '~/lib/start.coffee'
import ipc from '~/lib/ipc.coffee'
import video_desktop from '~/lib/video_desktop.coffee'
import config from '~/store/record.coffee'
import media_by_id from '~/lib/media_by_id.coffee'
import user from '~/lib/user.coffee'
import {HTTP} from '@/config/site.coffee'
import 统计点击 from '~/lib/trace/click.coffee'

{auto_update} = ipc

export default {
components:{
  src
  rArea
  drive
  btn
}
setup:=>
  开始录制 = '开始录制'
  txt = shallowRef(开始录制)

  授权 = (w)=>
    txt.value = "请授权#{w}后点此录制"
    return

  {
    txt
    start:=>
      for [en,cn] from Object.entries {
        audio:'麦克风'
        video:'摄像头'
      }
        id = config[en]
        if id
          try
            await media_by_id[en] id
          catch err
            授权 cn
            return

      if config.src != 'camera'
        try
          await video_desktop()
        catch err
          授权 '录屏'
          return

      localStorage.recing = 1
      {
        avatarUrl
        space:{used,max}
      } = await user.json('user/get')
      localStorage.ico = avatarUrl
      if used >= max
        txt.value = "请付费扩容后点此录制"
        ipc.open HTTP+'?pay=true&user_token='+localStorage.uid
      else
        txt.value = 开始录制
        统计点击.录
        start()
      return
  }
}
do =>
  await auto_update.update_promise()
    #if location.hash != '#main/about'
    #  location.hash = "main/update"
  return
</script>
